home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvi2qms / pxltoqms.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  4KB  |  160 lines

  1. /*
  2.  *    xtoq - TeX82 font to QMS font.
  3.  *
  4.  *    xtoq infile > outfile
  5.  */
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <local/qfont.h>
  10. #define MIN(x,y) ((x)<(y)? (x): (y))
  11. #define MAX(x,y) ((x)>(y)? (x): (y))
  12. #define RND(x)    ((int)((x)+0.5))
  13. #define MAGIC    1001
  14. #define    HIGH    0x80000000
  15.  
  16. char *progname;
  17. char *filename;
  18. short x_height, x_width;
  19. short x_voffset, x_hoffset;
  20. long x_pointer;
  21. long x_spacing;
  22. long dirbuf[4];
  23. int File;
  24. int maxheight;
  25. unsigned char thischar;
  26. struct q_header qh;
  27. struct q_glyph qg;
  28.  
  29. char *
  30. unctrl(c)
  31. unsigned char c;
  32. {
  33.     static char hold[3];
  34.     if (c == '\177') return "^?";
  35.     if (c<' ') sprintf(hold,"^%c", c^0100);
  36.     else sprintf(hold,"%c",c);
  37.     return hold;
  38. }
  39.  
  40. main(argc,argv)
  41. int argc;
  42. char **argv;
  43. {
  44.     int k, vert;
  45.     int i, j, raslen, outraslen;
  46.     long *buf, *bp;
  47.     long trailer[5];
  48.     progname = *argv;
  49.     while (--argc) {
  50.         if ('-'==**++argv) switch (*++*argv) {
  51.         default:
  52.             bomb();
  53.         }
  54.         else break;
  55.     }
  56.     if (!argc) bomb();
  57.     filename = *argv;
  58.     if (--argc>0) bomb();
  59.     File = open(filename,0);
  60.     if (0>File) {
  61.         fprintf(stderr,"%s: cannot read %s\n", progname,filename);
  62.         exit(1);
  63.     }
  64.     lseek(File,-5L*sizeof(long),2);
  65.     read(File,trailer,5*sizeof(long));
  66.     if (MAGIC != trailer[4]) {
  67.         fprintf(stderr,"%s: %s not a pxl file\n", progname,filename);
  68.         exit(1);
  69.     }
  70. /*
  71.  *    comb the directory to determine the QMS font height.
  72.  */
  73.     maxheight = 0;
  74.     lseek(File, trailer[3]*sizeof(long), 0);
  75.     for (thischar=0; thischar<128; thischar++) {
  76.         read(File, (char *)dirbuf, 4*sizeof(long));
  77.         x_height = dirbuf[0] & 0xffff;
  78.         x_pointer = dirbuf[2];
  79.         if (x_pointer) maxheight=MAX(maxheight, x_height);
  80.     }
  81. /*
  82.  *    write the QMS header.
  83.  */
  84.     qh.q_number = 0;
  85.     qh.q_orientation = 'P';
  86.     qh.q_version = '1';
  87.     strncpy(qh.q_name,filename,4);
  88.     qh.q_fheight = maxheight;
  89.     qh.q_baseline = 0;
  90.     qh.q_format = Q_FORMAT2;
  91.     qwriteh(stdout, &qh);
  92. /*
  93.  *    now convert the glyphs.
  94.  */
  95.     for (thischar=0; thischar<128; thischar++) {
  96.         lseek(File, (-5+(-128L + thischar)*4)*sizeof(long), 2);
  97.         read(File, (char *)dirbuf, 4*sizeof(long));
  98.         x_height = dirbuf[0] & 0xffff;
  99.         x_width = (dirbuf[0]>>16) & 0xffff;
  100.         x_voffset = dirbuf[1] & 0xffff;
  101.         x_hoffset = (dirbuf[1]>>16) & 0xffff;
  102.         x_pointer = dirbuf[2];
  103.         x_spacing = dirbuf[3];
  104.         if (!x_pointer && !x_spacing) continue;
  105.         raslen = (x_width+31) / 32;
  106.         if (!x_pointer) continue;    /* DON'T CONVERT SPACES */
  107. /*
  108.  *    allocate buffers.
  109.  */
  110.         qg.q_char = thischar;
  111.         qg.q_spacing =RND(300.*x_spacing*trailer[1]/(1000.*trailer[2]));
  112.         qg.q_width = x_width;
  113. /*
  114.  *    set the height to the glyph height.
  115.  *    the output file will have to be filtered through 'newheight'.
  116.  */
  117.         qg.q_height = x_height;
  118.         qg.q_hoffset = -x_hoffset;
  119.         qg.q_voffset = 37 + x_voffset - x_height;
  120.         outraslen = (qg.q_height + 7) / 8;
  121.         qg.q_bitmap = (unsigned char *)malloc(outraslen*qg.q_width);
  122.         for (k=0; k<outraslen*qg.q_width; k++) qg.q_bitmap[k]=0;
  123.         buf = (long *)malloc((unsigned)x_height*raslen*sizeof(long));
  124.         lseek(File, x_pointer*sizeof(long), 0);
  125.         read(File, (char *)buf, x_height * raslen * sizeof(long));
  126.         bp = buf;
  127.         for (i=0; i<x_height; i++) {
  128.             long dots;
  129.             for (j=0; j<x_width; j+=32) {
  130.                 dots = *bp++;
  131.                 vert = x_height - 1;
  132.                 for (k=0; k<32; k++) {
  133.                     if (32*j+k>=qg.q_width) break;
  134.                     if (HIGH & dots)
  135.                     qg.q_bitmap[(vert-i)/8+
  136.                         outraslen*(32*j+k)] |=
  137.                         1<<(7-(vert-i)%8);
  138.                     dots<<=1;
  139.                 }
  140.             }
  141.         }
  142.         qwrite(stdout,&qh,&qg);
  143.         free(buf);
  144.         free(qg.q_bitmap);
  145.     }
  146.     qend(stdout);
  147.     exit(0);
  148. }
  149.  
  150. bomb()
  151. {
  152.     fprintf(stderr,"usage: %s file\n",progname);
  153.     exit(1);
  154. }
  155. -- 
  156. Col. G. L. Sicherman
  157. ...{rocksvax|decvax}!sunybcs!colonel
  158.  
  159.  
  160.